//配置路由的地方
import Vue from 'vue';
import VueRouter from "vue-router";
//使用插件
Vue.use(VueRouter);
//引入路由组件
// import Home from '@/pages/Home'
import Layout from '@/pages/layout/index.vue'
import HomeIndex from '@/pages/home/index.vue'
import AboutIndex from '@/pages/about/index.vue'
import ErrorPage from '@/pages/error/index.vue'
import MenuIndex from '@/pages/menu/index.vue'
//编辑和修改菜单路由
import MenuCreateOrEdit from '@/pages/menu/create-or-edit.vue'
//资源路由
import ResourceCategoryIndex from '@/pages/resource/index.vue'
//资源名单
import ResourceIndex from '@/pages/interfaceLimit/index.vue'
//分配资源
import RoleAllocResource from '@/pages/roles/alloc-resource.vue'
//员工
import WorkerIndex from '@/pages/worker/index.vue'

import Login from '@/pages/Login'
// import EmployeePageAdmins from '@/pages/employees/EmployeePageAdmins.vue'
import English from '@/pages/knowledges/english/English.vue'
import EmployeePages from '@/pages/employees/EmployeePages.vue';

// import RolesIndex from '@/pages/roles/index.vue';
// import SlideShow from '@/components/SlideShow'
// import EditPassword from '@/pages/EditPassword'
// import Register from "@/pages/Register.vue";
// import tallKnowledge from "@/pages/knowledges/tall_knowledge/tall.vue";
// import educationPower from "@/pages/knowledges/education_power/education.vue";
// import financialLife from "@/pages/knowledges/financial_life/financial.vue";
// import talkStream from "@/pages/knowledges/talk_stream/talk.vue";
// import virtualComputer from "@/pages/knowledges/virtual_computer/virtual.vue";
// import MessageBoard from "@/pages/messageBoard/MessageBoard.vue";
import SingleSHandle from "@/pages/employees/SingleSHandle.vue";
import EmployeeEdit from "@/pages/employees/EmployeeEdit.vue";


//配置路由
//这个{}没匹配到就匹配下面的,从上到下匹配
//谁在前,路由先执行谁
const router = new VueRouter({
    mode: 'hash',
    //配置路由
    routes: [
        //http://localhost:8082/#/login
        {
            name: 'login',
            path: "/login",
            component: Login
        },
        {
            name: '',
            path: "/",
            component: Layout,
            children: [
                {
                    path: "",
                    name: "home",
                    component: HomeIndex,
                    meta: {
                        requireAuth: true
                    }
                },
                {
                    path: "about",//http://localhost:8082/#/about
                    name: "about",
                    component: AboutIndex,
                    meta: {
                        requireAuth: true
                    }
                },
                //设置菜单列表的路由
                {
                    path: '/menu',
                    name: 'menu',
                    component: MenuIndex
                },
                //添加菜单页面的路由
                {
                    path: '/menu/create',
                    name: 'menu-create',
                    component: MenuCreateOrEdit
                },
                //编辑菜单页面的路由 :id可以是任意
                {
                    path: '/menu/:id/edit',
                    name: 'menu-edit',
                    component: MenuCreateOrEdit
                },
                //资源类别列表的路由 http://localhost:8082/#/resource-category
                {
                    path: '/resource-category',
                    name: 'resource-category',
                    component: ResourceCategoryIndex
                },
                //资源列表的路由 http://localhost:8082/#/resource-category
                {
                    path: '/source',
                    name: 'source',
                    component: ResourceIndex
                },
                {
                    path: "/roles",
                    name:"roles",
                    //懒加载组件
                    component: ()=>import('@/pages/roles/index.vue'),
                    meta:{title:'角色'}
                },
                //分配角色
                {
                    path: "/role/:roleId/alloc-menu",
                    name:"alloc-menu",
                    //懒加载组件
                    component: ()=>import('@/pages/roles/alloc-menu.vue'),
                    //把路由的组件的值当属性处理,不当路径参数  搭配组件的props写法
                    props:true,
                    meta:{title:'分配角色'}
                },
                //分配资源
                {
                    path: '/role/:roleId/alloc-resource',
                    name: 'alloc-resource',
                    component: RoleAllocResource,
                    //把路由的组件的值当属性处理,不当路径参数  搭配组件的props写法
                    props:true,
                    meta:{title:'角色-分配资源'}
                },
                //分配资源
                {
                    path: '/worker',
                    name: 'worker',
                    component: WorkerIndex,
                },
                //课程
                {
                    path: '/courses',
                    name: 'courses',
                    component: ()=>import('@/pages/course/index.vue'),
                },
                //课程
                {
                    path: '/courses',
                    name: 'courses',
                    component: ()=>import('@/pages/course/index.vue'),
                },
                //新建课程
                {
                    path: '/course/create',
                    name: 'course-create',
                    component: ()=>import('@/pages/course/create_update.vue'),
                },
                //编辑课程
                {
                    path: '/course/:courseId/edit',
                    name: 'course-edit',
                    component: ()=>import('@/pages/course/create_update.vue'),
                    //路由参数变成属性
                    props:true
                },
                {
                    path: "EmployeePages",
                    component: EmployeePages,
                },
                {
                    name: 'English',
                    path: "English",
                    component: English,

                },
                {
                    name: 'SingleSHandle',
                    path: "SingleSHandle",
                    component: SingleSHandle,
                },
                {
                    name: 'EmployeeEdit',
                    path: "EmployeeEdit",
                    component: EmployeeEdit,
                },
            ]
        },
        {
            name: 'error-page',
            path: "*",
            component: ErrorPage,
        }
        //
        // {
        //     name: 'EmployeePageAdmins',
        //     //query参数(不需要配置)
        //     path: "/EmployeePageAdmins",
        //     component: EmployeePageAdmins,
        //     meta: {show: false}
        // },

        // {
        //     path: "/SlideShow",
        //     component: SlideShow,
        //     meta: {show: false}
        // },
        // //重定向,在项目跑起来的时候,访问/,立马让他重定向到首页
        // //对于所有未匹配到的路径（*），重定向（redirect）到'/login'路径，即用户访问任何不存在的路径都会被导航到登录页面。
        // //这种做法常用于处理未知路径或者未授权访问的情况
        // {
        //     path: "*",
        //     redirect: '/login'
        // },
        // {
        //     name: 'EditPassword',
        //     path: '/EditPassword',
        //     component: EditPassword
        // },
        // {
        //     name: 'register',
        //     path: '/Register',
        //     component: Register
        // },
        // {
        //     name: 'tallKnowledge',
        //     path: '/tall',
        //     component: tallKnowledge
        // },
        // {
        //     name: 'talkStream',
        //     path: '/tall',
        //     component: talkStream
        // },
        // {
        //     name: 'educationPower',
        //     path: '/education',
        //     component: educationPower,
        //     meta: {show: true}
        // },
        // {
        //     name: 'virtualComputer',
        //     path: '/virtual',
        //     component: virtualComputer
        // },
        // {
        //     name: 'financialLife',
        //     path: '/financial',
        //     component: financialLife
        // },
        // {
        //     name: 'MessageBoard',
        //     path: '/MessageBoard',
        //     component: MessageBoard
        // }
    ]
})

//全局前置路由守卫-——————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    // debugger
    //如果用户登录成功,不允许再次登录,重定向到首页
    if (to.name === "login") {
        if (localStorage.getItem('tokenInfo') !== ''&& localStorage.getItem('tokenInfo') !== null) {
            next('/')
        }
    }
    //from当前地址 to跳转的地址
    console.log('前置路由守卫', to, from)
    //some遍历数组
    console.log(to.matched.some(r => r.meta.requireAuth));
    //判断是否需要鉴权
    if (to.meta.requireAuth === true) {
        // debugger
        if (localStorage.getItem('tokenInfo') !== ''&& localStorage.getItem('tokenInfo') !== null) {
            next()
        } else {
            alert('使用本系统之前请登录,谢谢!')
            // 假设首页的路由名称是 'home'，根据实际情况修改
            next(
                {
                    name: 'login',
                    query: {
                        redirect: to.fullPath
                    }
                }
            );
            return
        }
    } else {
        next()
    }

})

export default router;
